package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.ProjectFinalaudit;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ProjectFinalauditService;
import com.xpsoft.oa.service.project.ProjectService;
import com.xpsoft.oa.service.system.AppUserService;

public class FinalAuditFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(FinalAuditFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String proId = (String)pi.getVariable("projectFinalaudit.project.id");
		String managerUserId = (String)pi.getVariable("projectFinalaudit.manageUser.userId");
		BigDecimal preTotal = (BigDecimal)pi.getVariable("projectFinalaudit.preTotal");
		BigDecimal materialCost = (BigDecimal)pi.getVariable("projectFinalaudit.materialCost");
		BigDecimal percentage = (BigDecimal)pi.getVariable("projectFinalaudit.percentage");
		String dataList = (String)pi.getVariable("projectFinalaudit.dataList");
		
		ProjectFinalaudit projectFinalaudit = new ProjectFinalaudit();
		projectFinalaudit.setPreTotal(preTotal);
		projectFinalaudit.setMaterialCost(materialCost);
		projectFinalaudit.setPercentage(percentage);
		
		AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
		if(StringUtils.isNotBlank(managerUserId)){
			projectFinalaudit.setManageUser(appUserService.get(Long.valueOf(managerUserId)));
		}
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			projectFinalaudit.setProject(projectService.get(Long.valueOf(proId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		projectFinalaudit.setProcessRunId(processRun.getRunId());
		
		ProjectFinalauditService projectFinalauditService = (ProjectFinalauditService)AppUtil.getBean("projectFinalauditService");
		projectFinalauditService.saveProjectFinalaudit(projectFinalaudit,dataList);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
